//!!! This source file is for Arduino UNO!!!
/***************************************************************/
/* Water Motors ******************************************************/
/*
 MOTOR 1                 MOTOR 2                MOTOR 3
 0b0000 ->Very Dry       0b0100 ->Very Dry      0b1000 ->Very Dry
 0b0001 ->Dry            0b0101 ->Dry           0b1001 ->Dry
 0b0010 ->Medium         0b0110 ->Medium        0b1010 ->Medium
 0b0011 ->Wet            0b0111 ->Wet           0b1011 ->Wet
*/
/***************************************************************/
/* Main variables */
byte locationPin1 = 5;
byte locationPin2 = 6;
byte locationPin3 = 7;

byte readByte = 0;
int locationNumber = 0;
int soilHummidity = 0;

#define HUM_MASK 0b0011;
#define LOC_MASK 0b1100;
#define WATER_LVL_VERY_LOW 0
#define WATER_LVL_LOW      1
#define WATER_LVL_MEDIUM   2
#define WATER_LVL_HIGH     3
#define WATER_TIME_LOW     2000
#define WATER_TIME_MEDIUM  5000
#define WATER_TIME_HIGH    10000

void setup() 
{
    Serial.begin(9600);
    pinMode(locationPin1, OUTPUT);
    pinMode(locationPin2, OUTPUT);
    pinMode(locationPin3, OUTPUT);
}

void loop() 
{
  // Read the byte sent via bluetooth
  if (Serial.available() > 0) 
  {  
    readByte = Serial.read();  
    
    soilHummidity = readByte & HUM_MASK;
    locationNumber = readByte & LOC_MASK;
    
    switch (soilHummidity)
    { 
      case WATER_LVL_VERY_LOW:
        digitalWrite(locationNumber + locationPin1,HIGH);
        delay(WATER_TIME_HIGH);
        digitalWrite(locationNumber + locationPin1,LOW);
        break;
      case WATER_LVL_LOW:
        digitalWrite(locationNumber + locationPin1,HIGH);
        delay(WATER_TIME_MEDIUM);
        digitalWrite(locationNumber + locationPin1,LOW);
        break;
      case WATER_LVL_MEDIUM:
        digitalWrite(locationNumber + locationPin1,HIGH);
        delay(WATER_TIME_LOW);
        digitalWrite(locationNumber + locationPin1,LOW);
        break;
      case WATER_LVL_HIGH:
        // no need for water anymore
        break;
      
      default: Serial.println("Error, invalid reading!");
      break;
    }
  }
}
